package cn.gok;

import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.TreeSet;

class School implements Serializable {
    private String name;
    private String address;
    private int birthYear;

    public School(String name, String address, int birthYear) {
        this.name = name;
        this.address = address;
        this.birthYear = birthYear;
    }

    @Override
    public String toString() {
        return "School{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", birthYear=" + birthYear +
                '}';
    }
}

class Student implements Comparable<Student> {
    private int sid;
    private String name;
    private int age;

    public Student(int sid, String name, int age) {
        this.sid = sid;
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sid=" + sid +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public int compareTo(Student o) {
        if (this.sid < o.sid) {
            return -1;
        } else if (this.sid == o.sid) {
            if (this.age < o.age) {
                return -1;
            }
        }
        return 1;
    }
}

class MyCompartor implements Comparator<Student> {
    @Override
    public int compare(Student s1, Student s2) {
        if (s1.getSid() < s2.getSid()) {
            return -1;
        } else if (s1.getSid() == s2.getSid()) {
            if (s1.getAge() < s2.getAge()) {
                return -1;
            }
        }
        return 1;
    }
}

class Pet {
    private String name;
    private int feetCount;

    public Pet() {
    }

    public Pet(String name, int feetCount) {
        this.name = name;
        this.feetCount = feetCount;
    }

    @Override
    public String toString() {
        return "Pet{" +
                "name='" + name + '\'' +
                ", feetCount=" + feetCount +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getFeetCount() {
        return feetCount;
    }

    public void setFeetCount(int feetCount) {
        this.feetCount = feetCount;
    }
}

class Family {

    private String owner;
    private ArrayList<Pet> pets;

    public Family(String owner, ArrayList<Pet> pets) {
        this.owner = owner;
        this.pets = pets;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public ArrayList<Pet> getPets() {
        return pets;
    }

    public void setPets(ArrayList<Pet> pets) {
        this.pets = pets;
    }

    @Override
    public String toString() {
        return "Family{" +
                "owner='" + owner + '\'' +
                ", pets=" + pets +
                '}';
    }
}

public class 作业五 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        writeToFIle();
        writeFromFIle();
        System.out.println("---------------");
        TreeSet<Student> set = new TreeSet<>();
        set.add(new Student(4, "BB", 20));
        set.add(new Student(4, "AA", 18));
        set.add(new Student(4, "CC", 19));
        set.stream().forEach(System.out::println);
        System.out.println("------------------");
        TreeSet<Student> set1 = new TreeSet<>(new MyCompartor());
        set1.add(new Student(4, "DD", 20));
        set1.add(new Student(4, "EE", 18));
        set1.add(new Student(4, "FF", 19));
        set1.stream().forEach(System.out::println);
        Family f1 = new Family("AA", new ArrayList<Pet>());
        Pet p1 = new Pet("duck", 2);
        Pet p2 = new Pet("cat", 4);
        f1.getPets().add(p1);
        f1.getPets().add(p2);
        Family f2 = new Family("BB", new ArrayList<>());
        Pet p3 = new Pet("spider", 6);
        Pet p4 = new Pet("cat1", 4);
        Pet p5 = new Pet("dog", 4);
        f2.getPets().add(p3);
        f2.getPets().add(p4);
        f2.getPets().add(p5);
        switchPets(f1, f2);
        System.out.println(f1);
        System.out.println(f2);
    }

    public static void switchPets(Family f1, Family f2) {
        int i1 = getPeetIndex(f1);
        int i2 = getPeetIndex(f2);
        Pet pet1 = getFootMax(f1);
        Pet pet2 = getFootMax(f2);
        f1.getPets().set(i1, pet2);
        f2.getPets().set(i2, pet1);
    }

    private static int getPeetIndex(Family f1) {
        for (int i = 0; i < f1.getPets().size(); i++) {
            if (f1.getPets().get(i).getFeetCount() == getFootMax(f1).getFeetCount()) {
                return i;
            }
        }
        return 0;
    }

    public static Pet getFootMax(Family f) {
        Pet pet = new Pet();
        for (int i = 0; i < f.getPets().size(); i++) {
            if (f.getPets().get(i).getFeetCount() > pet.getFeetCount()) {
                pet = f.getPets().get(i);
            }
        }
        return pet;
    }

    public static void writeToFIle() throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("F:\\idea\\rrrrr\\day4\\src\\cn\\gok\\1.dat"));
        oos.writeObject(new School("AA", "BB", 2002));
        oos.writeObject(new School("CC", "DD", 2010));
        oos.close();
        ;
        System.out.println("OK！");
    }

    public static void writeFromFIle() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("F:\\idea\\rrrrr\\day4\\src\\cn\\gok\\1.dat"));
        try {
            while (true) {
                Object obj = ois.readObject();
                if (obj instanceof School) {
                    System.out.println((School) obj);
                }
            }
        } catch (EOFException e) {
            System.out.println("OK!");
        }
        ois.close();
    }
}

